<template>
  <div class="login-container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login-form"
          ref="loginFormRef"
          :model="loginForm"
          :rules="rules"
        >
          <h1>Hello</h1>
          <h2>欢迎来到后台管理系统</h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              show-password
              :prefix-icon="Lock"
              v-model="loginForm.password"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              class="login-btn"
              type="primary"
              :loading="loading"
              @click="handleLogin"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script lang="ts" setup>
import { User, Lock } from '@element-plus/icons-vue'
import { ref, reactive } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import useUserStore from '@/store/modules/user.ts'
import { ElNotification } from 'element-plus'
import { getTime } from '@/utils/time'
let $router = useRouter()
let $route = useRoute()
let useStore = useUserStore()
// 校验规则
// 用户名校验方法
const validatorUserName = (rule: any, value: any, callback: any) => {
  if (value.length >= 5 && value.length <= 10) {
    callback()
  } else {
    callback(new Error('请输入5~10位用户名'))
  }
}

const validatorPassword = (rule: any, value: any, callback: any) => {
  if (value.length >= 6 && value.length <= 10) {
    callback()
  } else {
    callback(new Error('请输入6~10位密码'))
  }
}

const rules = {
  username: [
    {
      required: true,
      trigger: 'change',
      validator: validatorUserName,
    },
  ],
  password: [
    {
      required: true,
      trigger: 'change',
      validator: validatorPassword,
    },
  ],
}

// 数据
let loginForm = reactive({
  username: 'admin',
  password: '111111',
})
let loading = ref(false)
let loginFormRef = ref()
// 方法
// 登录按钮点击事件
const handleLogin = async () => {
  await loginFormRef.value.validate()

  loading.value = true
  try {
    // 登录接口
    await useStore.userLogin(loginForm)

    let redirect: any = $route.query.redirect
    $router.push({ path: redirect || '/' })

    ElNotification({
      type: 'success',
      message: '欢迎回来',
      title: `HI,${getTime()}好`,
    })

    loading.value = false
  } catch (error) {
    ElNotification({
      type: 'error',
      message: (error as Error).message,
    })

    loading.value = false
  }
}
</script>

<style scoped lang="scss">
.login-container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;

  .login-form {
    position: relative;
    margin: 0 auto;
    padding: 40px;
    width: 60%;
    top: 30vh;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;

    h1 {
      color: white;
      font-size: 40px;
    }

    h2 {
      color: white;
      font-size: 20px;
      margin: 20px 0;
    }

    .login-btn {
      width: 100%;
    }
  }
}
</style>
